﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JXT.PrimaryKey.Batman.Domain.Models
{
    [Serializable]
    public abstract class EntityBase<TKey> : IEntity
    {
        internal static readonly string IdentityName = "Id";

        public TKey Id { get; set; }

        public virtual string ToNameString()
        {
            return GetType().Name;
        }

        public virtual string ToUniqueKey()
        {
            return String.Format("{0}_{1}", ToNameString(), Id);
        }

        public virtual string GetIdentityString()
        {
            return Convert.ToString(Id);
        }

        public virtual string GetIdentityName()
        {
            return IdentityName;
        }

        public override bool Equals(object obj)
        {
            //return base.Equals(obj);
            if (obj == null || !(obj is IEntity))
                return false;
            return (obj as IEntity).ToUniqueKey() == ToUniqueKey();
        }

        public override int GetHashCode()
        {
            //return base.GetHashCode();
            return ToUniqueKey().GetHashCode();
        }
    }
}
